﻿<System.Management.Automation.Cmdlet("New", "Torrent", SupportsShouldProcess:=True, ConfirmImpact:=System.Management.Automation.ConfirmImpact.Medium)> _
Public Class NewTorrentCommand
    Inherits System.Management.Automation.Cmdlet

    <System.Management.Automation.Parameter(Mandatory:=True)> _
    Public Property Source As String

    <System.Management.Automation.Parameter(Mandatory:=True)> _
    Public Property SavePath As String

    <System.Management.Automation.Parameter> _
    Public Property TorrentName As String

    <System.Management.Automation.Parameter> _
    Public Property IgnoreHidden As System.Management.Automation.SwitchParameter

    <System.Management.Automation.Parameter> _
    Public Property MakePrivate As System.Management.Automation.SwitchParameter

    <System.Management.Automation.Parameter> _
    Public Property Announces As String()

    <System.Management.Automation.Parameter> _
    Public Property Comment As String

    <System.Management.Automation.Parameter> _
    Public Property CreatedBy As String

    <System.Management.Automation.Parameter> _
    Public Property Publisher As String

    <System.Management.Automation.Parameter> _
    Public Property PublisherUrl As String

    <System.Management.Automation.Parameter> _
    Public Property StoreMD5 As System.Management.Automation.SwitchParameter

    Protected Overrides Sub BeginProcessing()

        Dim TCreator As New MonoTorrent.Common.TorrentCreator()

        Dim TSource As New MonoTorrent.Common.TorrentFileSource(Me.Source, Me.IgnoreHidden.IsPresent)

        If String.Compare(System.IO.Path.GetExtension(Me.SavePath), ".TORRENT", True) <> 0 Then
            Me.WriteWarning("Torrent file should have .TORRENT file extension.")
        End If

        If Not String.IsNullOrEmpty(Me.TorrentName) Then
            TSource.TorrentName = Me.TorrentName
        End If

        If Not String.IsNullOrEmpty(Me.Comment) Then
            TCreator.Comment = Me.Comment
        End If

        If Not String.IsNullOrEmpty(Me.CreatedBy) Then
            TCreator.CreatedBy = Me.CreatedBy
        End If

        TCreator.Private = Me.MakePrivate.IsPresent

        If Not String.IsNullOrEmpty(Me.Publisher) Then
            TCreator.Publisher = Me.Publisher
        End If

        If Not String.IsNullOrEmpty(Me.PublisherUrl) Then
            TCreator.PublisherUrl = Me.PublisherUrl
        End If

        TCreator.StoreMD5 = Me.StoreMD5.IsPresent

        If Me.Announces IsNot Nothing Then
            For Each Announce In Me.Announces
                Dim AList As New System.Collections.Generic.List(Of String)

                AList.Add(Announce)
                TCreator.Announces.Add(AList)
            Next
        Else
            Dim announce1 As New System.Collections.Generic.List(Of String)
            Dim announce2 As New System.Collections.Generic.List(Of String)
            Dim announce3 As New System.Collections.Generic.List(Of String)

            announce1.Add("udp://tracker.openbittorrent.com:80/announce")
            announce2.Add("udp://tracker.publicbt.com:80/announce")
            announce3.Add("udp://tracker.ccc.de:80/announce")

            TCreator.Announces.Add(announce1)
            TCreator.Announces.Add(announce2)
            TCreator.Announces.Add(announce3)
        End If

        For Each Announce In TCreator.Announces
            For Each Address In Announce
                Me.WriteVerbose(String.Format("Announce: {0}", Address))
            Next
        Next

        If Me.ShouldProcess("torrent file", "write output") Then
            TCreator.Create(TSource, Me.SavePath)
        End If
    End Sub
End Class
